package com.fram.service.api.v2.impl;

import javax.transaction.Transactional;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.fram.bean.api.user.request.H5WechatLoginRequest;
import com.fram.bean.api.user.request.XcxLoginRequest;
import com.fram.bean.api.user.response.UserResponse;
import com.fram.common.auth.UserAuthMethod;
import com.fram.common.canstant.ErrCode;
import com.fram.common.exception.ProAffairException;
import com.fram.common.exception.ProRunTimeException;
import com.fram.common.util.BeanMapper;
import com.fram.common.util.json.JSONTools;
import com.fram.common.util.tencent.TencentLoginBean;
import com.fram.common.util.tencent.WechatAccessToken;
import com.fram.entity.User;
import com.fram.service.api.v1.impl.ApiUserServiceImpl;

/**
 * @作者： 陳曉鬆 @时间： 2020年10月14日 - 上午10:07:42 @description：->
 */
@Service("apiUserServiceV2")
public class ApiUserServiceImplV2 extends ApiUserServiceImpl {

	@Override
	@Transactional
	public UserResponse xcxLogin(XcxLoginRequest requestParam) throws ProAffairException {
		if (StringUtils.isEmpty(requestParam.getAppid()) || StringUtils.isEmpty(requestParam.getCode())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		TencentLoginBean tencentLoginBean = tencentUtils.getOpenId(requestParam.getAppid(), requestParam.getCode());
		if (tencentLoginBean == null || StringUtils.isEmpty(tencentLoginBean.getOpenid())) {
			throw new ProRunTimeException(ErrCode.USER_GETWECHAT_FAIL.getReturnCode(), ErrCode.USER_GETWECHAT_FAIL.getReturnMsg());
		}
		User user = userMapper.selectByAppidAndOpenid(requestParam.getAppid(), tencentLoginBean.getOpenid());
		boolean isLogin = false;
		if (user == null) {
			isLogin = true;
		} else if (StringUtils.isEmpty(user.getPhone())) {
			isLogin = true;
		}

		UserResponse response = new UserResponse();
		if (isLogin) {
			redisService.set(UserAuthMethod.LOGIN_REDIS_DB, UserAuthMethod.getBindPhoneRedisKey(requestParam.getAppid(), tencentLoginBean.getOpenid()),
					JSONTools.obj2json(tencentLoginBean) + ">>>" + JSONTools.obj2json(requestParam), 60 * 60 * 24);
		} else {
			setLoginToken(user);
			response = BeanMapper.map(user, UserResponse.class);
		}
		response.setOpenid(tencentLoginBean.getOpenid());
		return response;
	}

	@Override
	@Transactional
	public UserResponse h5WechatLogin(H5WechatLoginRequest request) throws ProAffairException {
		if (StringUtils.isEmpty(request.getAppid()) || StringUtils.isEmpty(request.getCode())) {
			throw new ProRunTimeException(ErrCode.ERR_PARAMETER.getReturnCode(), ErrCode.ERR_PARAMETER.getReturnMsg());
		}
		// 获取openid
		WechatAccessToken tencentLoginBean = tencentUtils.getH5OpenId(request.getAppid(), request.getCode());
		if (tencentLoginBean == null || StringUtils.isEmpty(tencentLoginBean.getOpenid())) {
			throw new ProRunTimeException(ErrCode.USER_GETWECHAT_FAIL.getReturnCode(), ErrCode.USER_GETWECHAT_FAIL.getReturnMsg());
		}
		User user = userMapper.selectByAppidAndOpenid(request.getAppid(), tencentLoginBean.getOpenid());
		boolean isLogin = false;
		if (user == null) {
			isLogin = true;
		} else if (StringUtils.isEmpty(user.getPhone())) {
			isLogin = true;
		}

		UserResponse response = new UserResponse();
		if (isLogin) {
			redisService.set(UserAuthMethod.LOGIN_REDIS_DB, UserAuthMethod.getBindPhoneRedisKey(request.getAppid(), tencentLoginBean.getOpenid()),
					JSONTools.obj2json(tencentLoginBean) + ">>>" + JSONTools.obj2json(request), 60 * 60 * 24);
		} else {
			setLoginToken(user);
			response = BeanMapper.map(user, UserResponse.class);
		}
		response.setOpenid(tencentLoginBean.getOpenid());
		return response;
	}

}
